package de.dopichaj.labrador.index.backend;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.PriorityQueue;

public final class MultiInvertedListSearcher {
    
    private final List<InvertedListSearcher> searchers;
    public MultiInvertedListSearcher(
        final List<InvertedListSearcher> searchers) {
        
        this.searchers = Collections.unmodifiableList(searchers);
    }
    
    private static final class TermAndIterator implements Comparable<TermAndIterator> {
        
        public final TermFrequency freq;
        public final Iterator<TermFrequency> iterator;
        
        public TermAndIterator(final Iterator<TermFrequency> iterator) {
            
            this.iterator = iterator;
            this.freq = iterator.next();
        }

        public int compareTo(TermAndIterator o) {
            return freq.compareTo(o.freq);
        }
    }
    
    public List<List<TermFrequency>> search(final Term term) throws IOException {
        
        // de.dopichaj.labrador.search and remember the lists
        final PriorityQueue<TermAndIterator> queue = new PriorityQueue<TermAndIterator>();
        for (final InvertedListSearcher searcher : searchers) {
            final Iterator<TermFrequency> firstResult = searcher.search(term);
            if (firstResult.hasNext()) {
                queue.add(new TermAndIterator(firstResult));
            }
        }
        
        // while there are results left
        final List<List<TermFrequency>> result = new ArrayList<List<TermFrequency>>();
        while (!queue.isEmpty()) {
            
            // determine the document ID for the next result set
            final int documentID = queue.peek().freq.getDocumentID();
            final List<TermFrequency> thisResult = new ArrayList<TermFrequency>();

            // for all results with the same document ID
            while (!queue.isEmpty() && queue.peek().freq.getDocumentID() == documentID) {
                
                // add them to the results
                final TermAndIterator tAndA = queue.poll();
                thisResult.add(tAndA.freq);
                
                // add the next value to the queue
                if (tAndA.iterator.hasNext()) {
                    queue.add(new TermAndIterator(tAndA.iterator));
                }
            }
            
            result.add(thisResult);
        }
        
        return result;
    }
}
/*
Copyright (c) 2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/